﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties.CApSLPSetLocationsPropertiesSourceLocationUtils
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties;

internal class CApSLPSetLocationsPropertiesSourceLocationUtils
{
  private TxObjEditBoxCtrl _sourceLocation;
  private ITxRoboticLocationOperation _previousSourceLocation;
  private ValidSourceLocationSelected _validSourceLocationSelectedReference;
  private InvalidSourceLocationSelected _invalidSourceLocationSelectedReference;
  private RefreshCommonPropertiesListDueToEvent _refreshCommonPropertiesListReference;

  public CApSLPSetLocationsPropertiesSourceLocationUtils(
    TxObjEditBoxCtrl sourceLocation,
    ITxRobotController controller,
    ValidSourceLocationSelected validSourceLocationSelectedReference,
    InvalidSourceLocationSelected invalidSourceLocationSelectedReference,
    RefreshCommonPropertiesListDueToEvent refreshCommonPropertiesListReference)
  {
    this._sourceLocation = sourceLocation;
    this.SetPickProviders();
    this.SetValidator(controller);
    this.RegisterEvents();
    this._sourceLocation.ListenToPick = true;
    this._validSourceLocationSelectedReference = validSourceLocationSelectedReference;
    this._invalidSourceLocationSelectedReference = invalidSourceLocationSelectedReference;
    this._refreshCommonPropertiesListReference = refreshCommonPropertiesListReference;
  }

  public void RegisterEvents()
  {
    this._sourceLocation.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this._sourceLocation_Picked);
    this._sourceLocation.TypeValid += new EventHandler(this._sourceLocation_TypeValid);
    this._sourceLocation.TypeInvalid += new EventHandler(this._sourceLocation_TypeInvalid);
  }

  public void UnRegisterEvents()
  {
    this.UnRegisterFromLocationsEvents(this._previousSourceLocation);
    this.UnRegisterFromLocationsEvents(this._sourceLocation.Object as ITxRoboticLocationOperation);
    this._previousSourceLocation = (ITxRoboticLocationOperation) null;
    this._sourceLocation.Object = (ITxObject) null;
  }

  private void SetPickProviders()
  {
    // ISSUE: unable to decompile the method.
  }

  private void SetValidator(ITxRobotController controller)
  {
    this._sourceLocation.SetValidator((ITxValidator) new CApSLPSetLocationsPropertiesLocationValidator(controller));
  }

  private void _sourceLocation_Picked(object sender, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    if (this._sourceLocation.Object == null)
    {
      this.UnRegisterFromLocationsEvents(this._previousSourceLocation);
      this._previousSourceLocation = (ITxRoboticLocationOperation) null;
      this._invalidSourceLocationSelectedReference();
    }
    else
    {
      this.UnRegisterFromLocationsEvents(this._previousSourceLocation);
      this._previousSourceLocation = this._sourceLocation.Object as ITxRoboticLocationOperation;
      this.RegisterToLocationsEvents(this._previousSourceLocation);
      this._validSourceLocationSelectedReference();
    }
  }

  private void _sourceLocation_TypeValid(object sender, EventArgs e)
  {
    this.UnRegisterFromLocationsEvents(this._previousSourceLocation);
    this._previousSourceLocation = this._sourceLocation.Object as ITxRoboticLocationOperation;
    this.RegisterToLocationsEvents(this._previousSourceLocation);
    this._validSourceLocationSelectedReference();
  }

  private void _sourceLocation_TypeInvalid(object sender, EventArgs e)
  {
    this.UnRegisterFromLocationsEvents(this._previousSourceLocation);
    this._previousSourceLocation = (ITxRoboticLocationOperation) null;
    this._invalidSourceLocationSelectedReference();
  }

  private void RegisterToLocationsEvents(ITxRoboticLocationOperation location)
  {
    if (location == null)
      return;
    ((ITxOperation) location).DescriptionSet += new TxOperation_DescriptionSetEventHandler(this.location_DescriptionSet);
    ((ITxOperation) location).DurationSet += new TxOperation_DurationSetEventHandler(this.location_DurationSet);
    ((ITxRoboticOperation) location).MountedWorkpieceOperationStateChanged += new TxRoboticOperation_MountedWorkpieceOperationStateChangedEventHandler(this.location_MountedWorkpieceOperationStateChanged);
    ((ITxObject) location).NameChanged += new TxObject_NameChangedEventHandler(this.location_NameChanged);
    ((ITxRoboticOperation) location).ParameterRemoved += new TxRoboticOperation_ParameterRemovedEventHandler(this.location_ParameterRemoved);
    ((ITxRoboticOperation) location).ParameterSet += new TxRoboticOperation_ParameterSetEventHandler(this.location_ParameterSet);
    location.RobotConfigurationSet += new TxRoboticLocationOperation_ConfigurationSetEventHandler(this.location_RobotConfigurationSet);
  }

  private void UnRegisterFromLocationsEvents(ITxRoboticLocationOperation location)
  {
    if (location == null)
      return;
    ((ITxOperation) location).DescriptionSet -= new TxOperation_DescriptionSetEventHandler(this.location_DescriptionSet);
    ((ITxOperation) location).DurationSet -= new TxOperation_DurationSetEventHandler(this.location_DurationSet);
    ((ITxRoboticOperation) location).MountedWorkpieceOperationStateChanged += new TxRoboticOperation_MountedWorkpieceOperationStateChangedEventHandler(this.location_MountedWorkpieceOperationStateChanged);
    ((ITxObject) location).NameChanged -= new TxObject_NameChangedEventHandler(this.location_NameChanged);
    ((ITxRoboticOperation) location).ParameterRemoved -= new TxRoboticOperation_ParameterRemovedEventHandler(this.location_ParameterRemoved);
    ((ITxRoboticOperation) location).ParameterSet -= new TxRoboticOperation_ParameterSetEventHandler(this.location_ParameterSet);
    location.RobotConfigurationSet -= new TxRoboticLocationOperation_ConfigurationSetEventHandler(this.location_RobotConfigurationSet);
  }

  private void location_RobotConfigurationSet(
    object sender,
    TxRoboticLocationOperation_ConfigurationSetEventArgs args)
  {
    if (this._refreshCommonPropertiesListReference == null)
      return;
    this._refreshCommonPropertiesListReference();
  }

  private void location_ParameterSet(object sender, TxRoboticOperation_ParameterSetEventArgs args)
  {
    if (this._refreshCommonPropertiesListReference == null)
      return;
    this._refreshCommonPropertiesListReference();
  }

  private void location_ParameterRemoved(
    object sender,
    TxRoboticOperation_ParameterRemovedEventArgs args)
  {
    if (this._refreshCommonPropertiesListReference == null)
      return;
    this._refreshCommonPropertiesListReference();
  }

  private void location_NameChanged(object sender, TxObject_NameChangedEventArgs args)
  {
    if (this._refreshCommonPropertiesListReference != null)
      this._refreshCommonPropertiesListReference();
    ((Control) this._sourceLocation).Text = this._sourceLocation.Object.Name;
  }

  private void location_MountedWorkpieceOperationStateChanged(
    object sender,
    TxRoboticOperation_MountedWorkpieceOperationStateChangedEventArgs args)
  {
    if (this._refreshCommonPropertiesListReference == null)
      return;
    this._refreshCommonPropertiesListReference();
  }

  private void location_DurationSet(object sender, TxOperation_DurationSetEventArgs args)
  {
    if (this._refreshCommonPropertiesListReference == null)
      return;
    this._refreshCommonPropertiesListReference();
  }

  private void location_DescriptionSet(object sender, TxOperation_DescriptionSetEventArgs args)
  {
    if (this._refreshCommonPropertiesListReference == null)
      return;
    this._refreshCommonPropertiesListReference();
  }
}
